//
//  GraphView.m
//  GraphingCalculator
//
//  Created by Willem van Willigen on 25/02/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GraphView.h"


@implementation GraphView

@synthesize delegate;





#define DEFAULT_SCALE 41

+ (BOOL)scaleIsValid:(CGFloat)aScale {
	return aScale > 1;
}

- (CGFloat)scale {
	return  [GraphView scaleIsValid:scale] ? scale : DEFAULT_SCALE;
}

- (void)setScale:(CGFloat)newScale {
	if([GraphView scaleIsValid:newScale]) {
		if(newScale != scale) {
			scale = newScale;
			[self setNeedsDisplay];
		}
	}
}






- (CGPoint)defaultMidPoint {
	CGPoint result;
	result.x = self.bounds.origin.x + self.bounds.size.width/2;
	result.y = self.bounds.origin.y + self.bounds.size.height/2;
	NSLog(@"defaultMidPoint");
	return result;
}

- (CGPoint)midPoint {
	return midPoint;
}

- (void)setMidPoint:(CGPoint)newMidPoint {
		if(newMidPoint.x != midPoint.x || newMidPoint.y != midPoint.y) {
			midPoint = newMidPoint;
			[self setNeedsDisplay];
		}
	
}

- (id)initWithFrame:(CGRect)frame pinchingAllowed:(BOOL)pinching panningAllowed:(BOOL)panning {
    if ((self = [super initWithFrame:frame])) {
		NSLog(@"GraphView initWithFrame");
		if(pinching) {
			UIGestureRecognizer *pinchgr = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
			[self addGestureRecognizer:pinchgr];
			[pinchgr release];
		}
		if(panning) {
			UIPanGestureRecognizer *pangr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
			UITapGestureRecognizer *tapgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
			tapgr.numberOfTapsRequired = 2;
			[self addGestureRecognizer:pangr];
			[self addGestureRecognizer:tapgr];
			[pangr release];
			[tapgr release];;
		}
		NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
		self.scale = [defaults floatForKey:@"scale"];
		NSLog(@"eerst dit natuurlijk");
		self.midPoint = CGPointMake([defaults floatForKey:@"midPointX"],[defaults floatForKey:@"midPointY"]);
		
		self.contentMode = UIViewContentModeRedraw;
    }
    return self;
}


- (void) pinch:(UIPinchGestureRecognizer *)gesture {
//	NSLog(@"pinch recognized!!");
	if ((gesture.state == UIGestureRecognizerStateChanged) ||
		(gesture.state == UIGestureRecognizerStateEnded)) {
		self.scale *= gesture.scale;
		gesture.scale = 1;
	}
	if (gesture.state == UIGestureRecognizerStateEnded) {
		NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
		[defaults setObject:[NSNumber numberWithFloat:scale] forKey:@"scale"];
		[defaults synchronize];
	}
}

- (void) pan:(UIPanGestureRecognizer *)gesture {
//	NSLog(@"pan recognized!!");
	if ((gesture.state == UIGestureRecognizerStateChanged) ||
		(gesture.state == UIGestureRecognizerStateEnded)) {
		CGPoint translation = [gesture translationInView:self];
		CGPoint newMidPoint = CGPointMake(self.midPoint.x+translation.x, self.midPoint.y+translation.y);
		self.midPoint = newMidPoint;
		[gesture setTranslation:CGPointZero inView:self];
	}
	if (gesture.state == UIGestureRecognizerStateEnded) {
		NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
		[defaults setObject:[NSNumber numberWithFloat:self.midPoint.x] forKey:@"midPointX"];
		[defaults setObject:[NSNumber numberWithFloat:self.midPoint.y] forKey:@"midPointY"];
		[defaults synchronize];
	}
}

- (void) tap:(UITapGestureRecognizer *)gesture {
	self.midPoint = [self defaultMidPoint];
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:[NSNumber numberWithFloat:self.midPoint.x] forKey:@"midPointX"];
	[defaults setObject:[NSNumber numberWithFloat:self.midPoint.y] forKey:@"midPointY"];
	[defaults synchronize];
//	NSLog(@"tap recognized!!");
}


- (void)drawRect:(CGRect)rect {
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	//draw axes
	//CGPoint midPoint;
	//CGRect test = self.bounds;
	//NSLog(@"drawRect bounds: %g %g %g %g",test.origin.x, test.origin.y, test.size.height, test.size.width);
	
	//NSLog(@"midpoint %g %g", midPoint.x, midPoint.y);

    [AxesDrawer drawAxesInRect:rect originAtPoint:midPoint scale:self.scale];
	
	//draw graph
	//NSLog(@"%f",self.bounds.size.width);
	CGContextBeginPath(context);
	float xBegin = (self.bounds.origin.x-midPoint.x) / self.scale;
	float yBegin = [delegate graphValueForGraphView:self xValue:xBegin];
	float convY = midPoint.y - (yBegin * self.scale);
	//NSLog(@"%f",yBegin);
	CGContextMoveToPoint(context, self.bounds.origin.x, convY);
	for(int i=self.bounds.origin.x+1; i<self.bounds.size.width; i++) {
		float xValue = (i-midPoint.x) / self.scale;
		float yValue = [delegate graphValueForGraphView:self xValue:xValue];
		float convertedY = midPoint.y - (yValue * self.scale);
		
			
		CGContextAddLineToPoint(context, i, convertedY);		
	}
	[[UIColor redColor] setStroke];
	CGContextDrawPath(context, kCGPathStroke);
	
}




- (void)dealloc {
    [super dealloc];
}


@end
